import { apiGetImgCaptcha } from '@/pages/_server/_api/Login/apiGetImgCaptcha';
import { apiGetMessageCode } from '@/pages/_server/_api/Login/apiGetMessageCode';
import Taro from '@tarojs/taro';
import { useState } from 'react';

export const useContent = (parmras) => {
    const { setImgCaptcha, closeImgCaptcha, setCaptchaText } = parmras;
    const getImgCaptcha = async (phoneNumber) => {
        console.log('设置验证码');
        const imgCaptchaNode = await apiGetImgCaptcha(phoneNumber);
        setImgCaptcha(imgCaptchaNode);
    };

    const canCel = () => {
        setCaptchaText('');
        closeImgCaptcha(false);
    };
    const confirm = async (phoneNumber, captchaText) => {
        // console.log(phoneNumber, captchaText);
        if (!captchaText)
            return Taro.showToast({
                title: '请输入验证码',
                icon: 'none',
                duration: 1000,
            });
        const res = await apiGetMessageCode({
            phoneNumber,
            captchaText,
        });
        if (!res) return getImgCaptcha(phoneNumber);
        setCaptchaText('');
        closeImgCaptcha(true);
    };

    return {
        getImgCaptcha,
        canCel,
        confirm,
    };
};
